<?
if (!defined('SMF'))
	die('Hacking attempt...');

if (preg_match("/check_mines.php/i", $_SERVER['PHP_SELF'])) {
	die("You can not access this file directly!");
}
	
global $smcFunc, $txt, $user_info;
global $dbtables, $mine_hullsize, $start_energy;

//Put the sector information into the array "sectorinfo"
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:sector_id}',
	array(
		'db_universe' => $dbtables['universe'],
		'sector_id' => $sector
	)
);
$sectorinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

//Put the defence information into the array "defenceinfo"  
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_sector_defence}
	WHERE sector_id = {int:sector_id}
	AND defence_type = "M"',
	array(
		'db_sector_defence' => $dbtables['sector_defence'],
		'sector_id' => $sector
	)
);

// Correct the targetship bug to reflect the player info
$targetship = $playerinfo;

$num_defences = 0;
$total_sector_mines = 0;
$owner = TRUE;

if ($smcFunc['db_num_rows']($result) > 0)
{
    while ($defences[$num_defences]= $smcFunc['db_fetch_assoc']($result))
	{
		$total_sector_mines += $defences[$num_defences]['quantity'];
		if ($defences[$num_defences]['ship_id'] != $playerinfo['ship_id'])
		{
			$owner = FALSE;
		}
		$num_defences++;
	}
}
$smcFunc['db_free_result']($result);

// Compute the ship average...if its too low then the ship will not hit mines...     
$shipavg = ($targetship['hull'] + $targetship['engines'] + $targetship['power'] + $targetship['computer'] + $targetship['sensors'] + $targetship['armor'] + $targetship['shields'] + $targetship['beams'] + $targetship['torp_launchers'] + $targetship['cloak']) / 10;

// The mines will attack if 4 conditions are met
//    1) There is at least 1 group of mines in the sector
//    2) There is at least 1 mine in the sector 
//    3) You are not the owner or on the team of the owner - team 0 dosent count
//    4) You ship is at least $mine_hullsize (setable in config.php) big

if ($num_defences > 0 && $total_sector_mines > 0 && !$owner && $shipavg > $mine_hullsize)
{
	// find out if the mine owner and player are on the same team
	$fm_owner = $defences[0]['ship_id'];

	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE ship_id = {int:fm_owner}',
		array(
			'db_ships' => $dbtables['ships'],
			'fm_owner' => $fm_owner
		)
	);
	$mine_owner = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	if ($mine_owner['team'] != $playerinfo['team'] || $playerinfo['team'] == 0)
	{
		// Well...you hit mines, shame...
		bigtitle($txt['bnt_chm_youencountermines']);
		
		$ok = 0;
		$totalmines = 0;
		
		// New Behavior
		// Before we had a issue where if there where a lot of mines in the sector the result will go -
		// I changed the behaivor so that rand will chose a % of mines to attack will
		// (it will always be at least 5% of the mines or at the very least 1 mine);
		// and if you are very unlucky they all will hit you
		$pren = (rand(5, 100) / 100);
		$roll = round($pren * $total_sector_mines - 1) + 1;
		$totalmines = $totalmines - $roll;

		// Red Alert: You are hit sir!!! Tell the player and put it in the log
		$txt['bnt_chm_youhitsomemines'] = str_replace("[chm_roll]", $roll, $txt['bnt_chm_youhitsomemines']);
		echo $txt['bnt_chm_youhitsomemines'], '<br>';
		playerlog($playerinfo['ship_id'], LOG_HIT_MINES, "$roll|$sector");

		// Tell the owner that his mines where hit
		$txt['bnt_chm_hehitminesinsector'] = str_replace("[chm_playerinfo_character_name]", $playerinfo['character_name'], $txt['bnt_chm_hehitminesinsector']);
		$txt['bnt_chm_hehitminesinsector'] = str_replace("[chm_roll]", "$roll", $txt['bnt_chm_hehitminesinsector']);
		$txt['bnt_chm_hehitminesinsector'] = str_replace("[chm_sector]", $sector, $txt['bnt_chm_hehitminesinsector']);

		message_defence_owner($sector,"$txt[bnt_chm_hehitminesinsector]");

		// If the player has enough mine deflectors then subtract the ammount and continue
		if ($playerinfo['dev_minedeflector'] >= $roll)
		{
			$txt['bnt_chm_youlostminedeflectors'] = str_replace("[chm_roll]", $roll, $txt['bnt_chm_youlostminedeflectors']);
			echo $txt['bnt_chm_youlostminedeflectors'], '<br>';
			$update = $smcFunc['db_query']('', '
				UPDATE {raw:db_ships}
				SET dev_minedeflector = dev_minedeflector - {int:roll}
				WHERE ship_id = {int:ship_id}',
				array(
					'db_ships' => $dbtables['ships'],
					'roll' => $roll,
					'ship_id' => $playerinfo['ship_id']
				)
			);
		}
		else
		{
			if ($playerinfo['dev_minedeflector'] > 0)
			{
				echo $txt['bnt_chm_youlostallminedeflectors'], '<br>';
			}
			else
			{
				echo $txt['bnt_chm_youhadnominedeflectors'], '<br>';
			}
		 
			// Shields up sir!
			$mines_left = $roll - $playerinfo['dev_minedeflector'];
			$playershields = NUM_SHIELDS($playerinfo['shields']);
			if ($playershields > $playerinfo['ship_energy'])
			{
				$playershields = $playerinfo['ship_energy'];
			}
			if ($playershields >= $mines_left)
			{
				$txt['bnt_chm_yourshieldshitforminesdmg'] = str_replace("[chm_mines_left]", $mines_left, $txt['bnt_chm_yourshieldshitforminesdmg']);
				echo $txt['bnt_chm_yourshieldshitforminesdmg'], '<br>';
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_ships}
					SET ship_energy = ship_energy - {int:mines_left},
						dev_minedeflector = 0
					WHERE ship_id = {int:ship_id}',
					array(
						'db_ships' => $dbtables['ships'],
						'mines_left' => $mines_left,
						'ship_id' => $playerinfo['ship_id']
					)
				);
				if ($playershields == $mines_left)
					echo $txt['bnt_chm_yourshieldsaredown'], '<br>';
			}
			else
			{
				// Direct hit sir
				echo $txt['bnt_chm_youlostallyourshields'], '<br>';
				$mines_left = $mines_left - $playershields;
				if($playerinfo['armor_pts'] >= $mines_left)
				{
					$txt['bnt_chm_yourarmorhitforminesdmg'] = str_replace("[chm_mines_left]", $mines_left, $txt['bnt_chm_yourarmorhitforminesdmg']);
					echo $txt['bnt_chm_yourarmorhitforminesdmg'], '<br>';
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET armor_pts = armor_pts - {int:mines_left}, ship_energy = 0, dev_minedeflector = 0 
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'mines_left' => $mines_left,
							'ship_id' => $playerinfo['ship_id']
						)
					);
					if ($playerinfo['armor_pts'] == $mines_left)
						echo $txt['bnt_chm_yourhullisbreached'], '<br>';
				}
				else
				{
					// BOOM
					$pod = $playerinfo['dev_escapepod'];

					playerlog($playerinfo['ship_id'], LOG_SHIP_DESTROYED_MINES, "$sector|$pod");
					$txt['bnt_chm_hewasdestroyedbyyourmines'] = str_replace("[chm_playerinfo_character_name]", $playerinfo['character_name'], $txt['bnt_chm_hewasdestroyedbyyourmines']);
					$txt['bnt_chm_hewasdestroyedbyyourmines'] = str_replace("[chm_sector]", $sector, $txt['bnt_chm_hewasdestroyedbyyourmines']);

					message_defence_owner($sector,"$txt[bnt_chm_hewasdestroyedbyyourmines]");

					echo $txt['bnt_chm_yourshiphasbeendestroyed'], '<br><br>';

					if ($playerinfo['dev_escapepod'] == "Y")
					{
						// Live . . .
						$rating = round($playerinfo['rating'] / 2);
						echo $txt['bnt_chm_luckescapepod'], '<br><br>';
						$update = $smcFunc['db_query']('', '
							UPDATE {raw:db_ships}
							SET hull = 0,
								engines = 0,
								power = 0,
								sensors = 0,
								computer = 0,
								beams = 0,
								torp_launchers = 0,
								torps = 0,
								armor = 0,
								armor_pts = 100,
								cloak = 0,
								shields = 0,
								sector = 0,
								ship_organics = 0,
								ship_ore = 0,
								ship_goods = 0,
								ship_energy={int:start_energy},
								ship_colonists = 0,
								ship_fighters = 100,
								dev_warpedit = 0,
								dev_genesis = 0,
								dev_beacon = 0,
								dev_emerwarp = 0,
								dev_escapepod = "N",
								dev_fuelscoop = "N",
								dev_minedeflector = 0,
								on_planet = "N",
								rating = {int:rating},
								cleared_defences = NULL,
								dev_lssd = "N"  
							WHERE ship_id = {int:ship_id}',
							array(
								'db_ships' => $dbtables['ships'],
								'start_energy' => $start_energy,
								'rating' => $rating,
								'ship_id' => $playerinfo['ship_id']
							)
						);
						cancel_bounty($playerinfo['ship_id']);
					}
					else
					{
						// or die!
						cancel_bounty($playerinfo['ship_id']);
						db_kill_player($playerinfo['ship_id']);
					}
				}
			}
		}
		explode_mines($sector,$roll);
	}
}
?>



